package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//@Service 这里不能加servcie  ，因为泛型T不是一个确定的类型
//事务控制-加了之后我们的实现类不需要加
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T findOne(Long id) {
        return baseMapper.loadOne(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Transactional
    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //1.先查询总数量
        Integer count = baseMapper.queryCount(baseQuery);
        //2.判断
        if(count == null || count == 0){
            return new PageList<T>();
        }
        //3.查询当前页的数据
        List<T> list = baseMapper.queryList(baseQuery);
        return new PageList<T>(count,list);
    }

}
